package com.yanq.liancar.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yanq.liancar.R;
import com.yanq.liancar.adapter.CarAdapter;
import com.yanq.liancar.bean.CarBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.net.util.ProgressSubscriber;
import com.yanq.liancar.util.ToastUtil;
import com.yanq.liancar.view.popupwindow.BasePopupWindow;
import com.yanq.liancar.view.popupwindow.OnPositiveListener;

import java.util.ArrayList;
import java.util.List;


public class PopWinCar extends BasePopupWindow implements View.OnClickListener {

    private OnPositiveListener onPositiveListener;
    private String driNum = null;
    private RecyclerView carRecycler;

    public void setOnPositiveListener(OnPositiveListener onPositiveListener) {
        this.onPositiveListener = onPositiveListener;
    }

    public PopWinCar(Context context) {
        super(context);
    }

    @Override
    public int layoutResId() {
        return R.layout.pop_win_car;
    }

    @Override
    public void init() {
        carRecycler = findViewById(R.id.car_recycler);
        TextView carNegative = findViewById(R.id.car_negative);
        TextView carPositive = findViewById(R.id.car_positive);
        carNegative.setOnClickListener(this);
        carPositive.setOnClickListener(this);

        mHttpUtil.getCar(new ProgressSubscriber<CarBean>(mContext) {
            @Override
            public void next(CarBean carBean) {
                List<CarBean.DataBean> data = carBean.getData();
                if (data != null && data.size() > 0) {
                    carRecycler.setLayoutManager(new LinearLayoutManager(mContext));
                    CarAdapter carAdapter = new CarAdapter(mContext, data);
                    carAdapter.setOnPositiveListener(new OnPositiveListener() {
                        @Override
                        public void onPositive(String value) {
                            driNum = value;
                        }
                    });
                    carRecycler.setAdapter(carAdapter);
                }
            }
        }, UserBean.getcToken(mContext));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.car_negative:
                myDismiss();
                break;
            case R.id.car_positive:
                if (driNum != null) {
                    if (onPositiveListener != null) {
                        onPositiveListener.onPositive(driNum);
                        myDismiss();
                    }
                } else {
                    ToastUtil.show(mContext, "请选择车牌号");
                }
                break;
        }
    }
}